﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.Logic.POI;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;
using Action = TreeSharp.Action;
using Sequence = TreeSharp.Sequence;
using Styx.WoWInternals;


namespace Healz.Druid
{
    public static class Main
    {
        public static Config cSettings;

        // So we can heal more than 1 person at a time ( lowest health )
        // Just say they have every hot on them, go to next person      
        public static void Pulse(int ElementAt)
        {
            // Just do check @ first element
            // Save some CPU i guess? not that its needed lol
            if (ElementAt == 0)
            {
                // Checks for no good
                if (StyxWoW.GlobalCooldown) return;
                if (StyxWoW.Me.IsCasting) return;
                if (ObjectManager.Me.ChanneledCastingSpellId != 0) return;
                if (StyxWoW.Me.Stunned) return;
                if (StyxWoW.Me.Dead) return;
                if (StyxWoW.Me.Mounted) return;
                if (StyxWoW.Me.ManaPercent <= 7) return;


                // check our global thing
                if (Spells.TreeofLife.Pulse()) return;
                if (Spells.Innervate.Pulse()) return;
                if (Spells.WildGrowth.Pulse()) return;
            }

            // Get our best player
            WoWPlayer BestPlayer = PlayerManager.PlayerToHeal(ElementAt);

            // No one to heal
            // Other Junk / MOTW, Deal a little damage i guess 
            if (BestPlayer == null)
            {
                // Save our mana 
                if (StyxWoW.Me.ManaPercent > 70)
                {
                    if (Spells.Corruption.Pulse()) return;
                    if (Spells.MarkoftheWild.Pulse()) return;
                }

                // All Done
                return;
            }

            // A little debug
            // For now just to see how many elements it actually goes threw.
            // Seems to be working way i want it
            Utils.WriteDebug("[{0}] - Player: {1}  - [{2}%]", ElementAt, BestPlayer.Name, (int)BestPlayer.HealthPercent);

            // Ok lets now heal
            CheckDoHeal(BestPlayer, ElementAt);
        }

        public static void CheckDoHeal(WoWPlayer Player, int ElementAt)
        {
            // check for people who are in serious trouble
            if (Spells.Swiftmend.Pulse(Player)) return;
            if (Spells.NaturesSwiftness.Pulse(Player)) return;

            // Rejuv should always be on IMO
            if (Spells.Rejuvenation.Pulse(Player)) return;

            // We have tree of life, Smash Regrowth - Insta heal
            if (MySpellManager.HasMyAura(StyxWoW.Me, "Tree of Life"))
            {
                if (Spells.Regrowth.Pulse(Player)) return;
            }

            // Lifebloom
            if (Spells.Lifebloom.Pulse(Player)) return;

            // 1 in 3 chance of running regrowth
            if (Spells.Regrowth.RandomPulse(Player)) return;

            // We got to end fucken
            // Means we didnt cast anything fucken, Pulse again at next element fucken
            ElementAt += 1;
            Pulse(ElementAt);
        }

    }
}
